package win.skxe07.web;

import java.util.List;

import org.quartz.SchedulerException;
import org.quartz.Trigger.TriggerState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import win.skxe07.quartz.QuartzManager;
import win.skxe07.quartz.ScheduleJob;

@RestController
public class HelloWeb {
	
	@Autowired
	QuartzManager quartzManager;
	
	@GetMapping("/hello")
	public String hello(String hour, String minute){
		ScheduleJob job = new ScheduleJob();
		job.setJobName("test1"+minute);
		job.setJobGroup("testdemo");
		job.setCronExpression("0 "+minute+"  "+hour+"  * * ?");
		quartzManager.addJob(job);
		return "OK";
	}
	
	@GetMapping("/list")
	public String list(){
		try {
			List<ScheduleJob> list = quartzManager.getAllJob();
			for (ScheduleJob scheduleJob : list) {
				System.out.println("ScheduleJob:"+scheduleJob.getJobName());
			}
			List<ScheduleJob> runList = quartzManager.getRunningJob();
			for (ScheduleJob scheduleJob : runList) {
				System.out.println("RUN---ScheduleJob:"+scheduleJob.getJobName());
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return "OK";
	}
	
	@GetMapping("/pause/{minute}")
	public ScheduleJob pauseJob(@PathVariable String minute){
		ScheduleJob job = new ScheduleJob();
		job.setJobName("test1"+minute);
		job.setJobGroup("testdemo");
		try {
			quartzManager.pauseJob(job);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return job;
	}
	
	@GetMapping("/isRun/{minute}")
	public TriggerState isStarted(@PathVariable String minute){
		ScheduleJob job = new ScheduleJob();
		job.setJobName("test1"+minute);
		job.setJobGroup("testdemo");
		TriggerState s = quartzManager.jobIsRun(job);
		return s;
	}
	
	

}
